﻿using System;
using System.Net.Mime;
using System.Text;
using System.Web;
using System.Web.Mvc;
using MiniWeb.Core.Dtos;
using MiniWeb.Core.Entities;
using MiniWeb.Core.Enums;
using MiniWeb.M.Code;
using MiniWeb.M.Controllers.ViewModels;
using MiniWeb.Task;
using System.Linq;
using WFramework;
using WFramework.Common;
using WFramework.Email;
using WFramework.MvcPager;
using WFramework.Utility;

namespace MiniWeb.M.Controllers
{
    public class HomeController : UserBaseController
    {
        //
        // GET: /Home/
        private readonly WebLoginLogTask _webLoginLogTask = new WebLoginLogTask();
        private readonly AccountsInfoTask _accountsInfoTask = new AccountsInfoTask();
        private readonly AdConfigTask _adConfigTask = new AdConfigTask();

        public ActionResult Index()
        {
            var list = _adConfigTask.GetAll(null, "PlatForm = 1");
            ViewData["zhongbuad"] = list.Where(c => c.Position == 2).ToList();
            return View(list);
        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(UserLogOnModel model, string returnUrl)
        {

            if (ModelState.IsValid)
            {
                //if (!_validateCodeService.CheckCode(model.ValidCode))
                //{
                //    _validateCodeService.ClearSession();
                //    return AlertMsg("验证码不正确", Request.UrlReferrer.ToString());

                //}

                var accountModel = _accountsInfoTask.GetByAccount(model.Account);
                if (accountModel == null)
                {
                    ModelState.AddModelError("Account", "帐号不存在");
                    return View();
                }


                if (accountModel.Password != CryptTools.HashPassword(model.Pwd))
                {
                    ModelState.AddModelError("Account", "密码不正确");
                    return View();
                }

                accountModel.LastLoginIP = accountModel.ThisLoginIP;
                accountModel.LastLoginTime = accountModel.ThisLoginTime;

                accountModel.ThisLoginIP = DNTRequest.GetIP();
                accountModel.ThisLoginTime = DateTime.Now;
                _accountsInfoTask.Update(accountModel);

                // 添加登录日志
                _webLoginLogTask.CreateLog(model.Account, LoginTypeEnum.网站登录);
                WebFormsAuthService.SignIn(model.Account);
                return string.IsNullOrEmpty(returnUrl)
                         ? Redirect(Url.Action("Index", "Home"))
                         : Redirect(returnUrl);
            }

            ModelState.AddModelError("Account", "未知错误");
            return View();
        }


        public ActionResult Register()
        {
            var model = new RegisterModel();
            return View(model);
        }

        public ActionResult ExitsName(string account)
        {
            var exitesaccount = _accountsInfoTask.ExistsAccount(account);
            return Json(!exitesaccount);
        }

        [HttpPost]
        public ActionResult Register(RegisterModel model, string returnUrl)
        {

           
            if (ModelState.IsValid)
            {
                string msg = string.Empty;

                //if (!_validateCodeService.CheckCode(model.ValidCode))
                //    return AlertMsg("验证码不正确", Request.UrlReferrer.PathAndQuery);


                var accountmodel = new AccountsInfo
                {
                    Account = model.Account,
                    Password = CryptTools.HashPassword(model.Pwd),
                    HeadImg = "/img/defaultface.jpg",
                    RegIP = DNTRequest.GetIP(),
                    RegTime = DateTime.Now,
                    RegType = (int)RegTypeEnum.网站注册,
                    Phone = model.Phone,
                    Email = model.Email,
                };
                if (!_accountsInfoTask.Register(accountmodel, out msg))
                {

                    ModelState.AddModelError("Account",msg);
                    return View();
                };

                // 添加登录日志
                _webLoginLogTask.CreateLog(model.Account, LoginTypeEnum.网站登录);

                WebFormsAuthService.SignIn(model.Account);
                return RedirectToAction("Index", "Home");

            }
            ModelState.AddModelError("Account", "未知错误");
            return View();


        }

        public ActionResult Loginout()
        {
            WebFormsAuthService.SignOut();
            return RedirectToAction("Index", "Home");
        }

    }
}
